package com.jhhc.StormSurgeForecast.AstronomicalTide.Util;

import com.alibaba.fastjson2.JSONObject;
import com.jhhc.RHDB.Data.Pojo.Data;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_SEAWALL_B;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.SeaWallInfo;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.SeawallTideData;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_SEAWALL_BService;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_ASTROTD_A;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_SEAWALL_P;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_ASTROTD_AService;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_RESULT_AService;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_SEAWALL_PService;
import com.jhhc.utils.CalResult;
import com.jhhc.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class GetSeaWallUitl {
    @Autowired
     BDMS_FBC_SEAWALL_BService bdms_fbc_seawall_bService;
    @Autowired
    BDMS_FBC_SEAWALL_PService bdms_fbc_seawall_pService;
    @Autowired
    BDMS_FBC_ASTROTD_AService bdms_fbc_astrotd_aService;
    //获取堤防信息
    public  List<SeaWallInfo>  getSeaWallInfo(int Pid){
        List<SeaWallInfo> seaWall_infos = new ArrayList<>();
        List<BDMS_FBC_SEAWALL_B> bdms_fbc_seawall_bs = bdms_fbc_seawall_bService.selectByPid(Pid);
        for (BDMS_FBC_SEAWALL_B bdms_fbc_seawall_b:bdms_fbc_seawall_bs){
            SeaWallInfo seaWall_info = new SeaWallInfo();
            seaWall_info.setSeaWallId(bdms_fbc_seawall_b.getSwid());
            seaWall_info.setSeaWallName(bdms_fbc_seawall_b.getSwnm());
            seaWall_info.setSeaWallCode(bdms_fbc_seawall_b.getSwcd());
            seaWall_info.setStartlgtd(bdms_fbc_seawall_b.getStartlgtd());
            seaWall_info.setStartlttd(bdms_fbc_seawall_b.getStartlttd());
            seaWall_info.setEndlgtd(bdms_fbc_seawall_b.getEndlgtd());
            seaWall_info.setEndlttd(bdms_fbc_seawall_b.getEndlttd());
            seaWall_info.setElevationsys(bdms_fbc_seawall_b.getElevationsys());
            seaWall_info.setBankhMax(bdms_fbc_seawall_b.getBankhMax());
            seaWall_info.setBankhMin(bdms_fbc_seawall_b.getBankhMin());

            seaWall_infos.add(seaWall_info);
        }
        return seaWall_infos;
    }

    //根据开始时间结束时间海堤编号集合获取海堤天文潮
    public List<SeawallTideData> getTideDataBySeawall(String  startTime, String endTime, List<Integer> swIds){
        List<SeawallTideData> list = new ArrayList<>();
        //根据项目id 海堤编号获取
        List<BDMS_FBC_SEAWALL_B> bdms_fbc_seawall_bs = bdms_fbc_seawall_bService.selectByPidSwids(2205,swIds);
        List<BDMS_FBC_SEAWALL_P> bdms_fbc_seawall_ps = bdms_fbc_seawall_pService.selectGridByPidSwids1(2205,swIds,null);
        for (BDMS_FBC_SEAWALL_P bdms_fbc_seawall_p : bdms_fbc_seawall_ps) {
            SeawallTideData seawallTideData = new SeawallTideData();
            BDMS_FBC_SEAWALL_B seawallB= bdms_fbc_seawall_bs.stream().filter(bdms_fbc_seawall_b -> bdms_fbc_seawall_b.getSwid().equals(bdms_fbc_seawall_p.getSwid())).findAny().orElse(null);
            seawallTideData.setSeawallId(String.valueOf(bdms_fbc_seawall_p.getSwid()));
            if (seawallB != null) {
                seawallTideData.setSeawallName(seawallB.getSwnm());
            }
            seawallTideData.setGridId(bdms_fbc_seawall_p.getCellids());
            List<Data> datas =new ArrayList<>();
            Integer integer =null;
            if ("0".equals(bdms_fbc_seawall_p.getCellids())) {
                integer = 1;
            }else {
                integer= Integer.valueOf(bdms_fbc_seawall_p.getCellids());
            }
            List<BDMS_FBC_ASTROTD_A> bdms_fbc_astrotd_as= bdms_fbc_astrotd_aService.selectByGrid(2205,startTime,endTime,integer);
            for (BDMS_FBC_ASTROTD_A bdms_fbc_astrotd_a : bdms_fbc_astrotd_as) {
                Data data = new Data();
                data.setTM(bdms_fbc_astrotd_a.getTm());
                data.setValue(Double.valueOf(bdms_fbc_astrotd_a.getDtvalues()));
                datas.add(data);
            }
            seawallTideData.setDatas(datas);
            list.add(seawallTideData);
        }


        return list;
    }



}
